library(weights)

options(contrasts=c("contr.treatment", "contr.treatment"))

                                        #source("~/Box/2014_Panel_Data/recodemerged.r")
load("Data/FergusonReplicationData.rdata")

w1use <- nd$w1.completecase==TRUE & !is.na(nd$w1.completecase)
bwuse <- demog$w3.race %in% c("WhiteNH", "BlackNH") & !is.na(demog$w3.race)
use <- w1use & bwuse

df <- nd[use,]
demo <- demog[use,]
black <- demo$w3.race=="BlackNH"
white <- demo$w3.race=="WhiteNH"
party01 <- nalevs(party7[use])


#Locations of 2014 Respondents
library(maps)

lat <- df$w1.latitude
long <- df$w1.longitude

pdf("Figures/FigureA1-RespLocations.pdf", width=8, height=6)
map('state')
lines(long[!black], lat[!black], type="p", pch=1)
lines(long[black], lat[black], type="p", pch=19, col="dark gray")
legend(x="bottomleft", legend=c("Black", "White"), pch=c(19,1), col=c("dark gray", "black"))
dev.off()



# Racial Divide in Perceptions of Ferguson

perceptualqs <- with(df, data.frame(w1.wilsonshouldbecharged, w1.brownattackedcombo, w1.brownweapon, w1.raceroleinshooting, w1.protestappropriate, w1.responseappropriate))
pq01 <- as.data.frame(sapply(perceptualqs, nalevs))


percepnames <- c("Wilson Should Be Charged (Probably or Definitely)", "Brown Attacked Wilson (Very or Extremely Likely)", "Brown Had Weapon (Probably or Definitely)", "Role of Race In Shooting (Large or Enormous)", "Protests Appropriate", "Police Response Appropriate")

percepNs <- sapply(perceptualqs, function(x) c(sum(!is.na(x) & black==FALSE), sum(!is.na(x) & black==TRUE)))
rownames(percepNs) <- c("WhiteN", "BlackN")

perceptualdifs <- t(sapply(perceptualqs, function(x) unlist(wtd.t.test(nalevs(x)[black==FALSE], nalevs(x)[black==TRUE]))))

top2cats <- as.data.frame(sapply(perceptualqs, function(x) nalevs(x)>.5))

perceptualdifst2 <- cbind(as.data.frame(t(sapply(top2cats, function(x) unlist(wtd.t.test(x[black==FALSE], x[black==TRUE])))), stringsAsFactors=FALSE), t(percepNs))

perceptualdifst2$adjusted.p.values <- p.adjust(perceptualdifst2$coefficients.p.value, "BY")

perceptualdifstable <- data.frame(Whites=perceptualdifst2[,"additional.Mean.x"], Blacks=perceptualdifst2[,"additional.Mean.y"], Difference=perceptualdifst2[,"additional.Difference"], sig=starmaker(as.numeric(as.character(perceptualdifst2[,"coefficients.p.value"]))))

rownames(perceptualdifstable) <- percepnames

write.csv(perceptualdifstable, "Tables/Table1-FergPerceptionsbyRace.csv")
write.csv(perceptualdifst2, "Tables/TableC1-AdditionalInfoOnFergPerceptionsByRace.csv")


# Regression-Based Analyses

heardferg <- nalevs(df$w1.heardferg)

regset <- lapply(pq01, function(y) lm(y~black*heardferg))

df$trstpol <- (nalevs(df$w1.trstpolice)+nalevs(df$w1.confidpolice))/2



age01 <- nalevs(demo$w3.age)
#party01 <- nalevs(df$w1.party7pt)
libcon01 <- nalevs(df$w1.libconcombo)
ed01 <- nalevs(demo$w3.educ)
fem <- demo$w3.female

df$racres <- rowMeans(with(df, data.frame(nalevs(w2.othersovercameprej), nalevs(w2.tryharder), 1-nalevs(w2.difficulttoovercome), 1-nalevs(w2.blacksgotmoreeconomically), nalevs(w2.pushspeed), nalevs(w2.blackresponsibility), 1-nalevs(w2.discrimamt))))




regsetcontrolsnoheard <- lapply(pq01, function(y) lm(y~black+fem+age01+ed01+party01+libcon01))

regsetcontrols <- lapply(pq01, function(y) lm(y~black*heardferg+fem+age01+ed01+party01+libcon01))

Ns <- sapply(regset, function(x) length(predict(x)))
R2s <- sapply(regset, function(x) summary(x)$r.squared)

ext <- function(x)
    cbind(Coef=coef(summary(x))[,"Estimate"], SE=coef(summary(x))[,"Std. Error"], p=coef(summary(x))[,"Pr(>|t|)"], padj=p.adjust(coef(summary(x))[,"Pr(>|t|)"], "BY"))

regout <- rbind(names(regset)[1], ext(regset[[1]]), N=Ns[1], R2=R2s[1])
for(i in 2:length(regset)){
    regtemp <- rbind(names(regset)[i], ext(regset[[i]]), N=Ns[i], R2=R2s[i])
    regout <- cbind(regout, regtemp)
}

Nscontrols <- sapply(regsetcontrols, function(x) length(predict(x)))
R2scontrols <- sapply(regsetcontrols, function(x) summary(x)$r.squared)

regoutcontrols <- rbind(names(regsetcontrols)[1], ext(regsetcontrols[[1]]), N=Nscontrols[1], R2=R2scontrols[1])
for(i in 2:length(regset)){
    regtemp <- rbind(names(regsetcontrols)[i], ext(regsetcontrols[[i]]), N=Nscontrols[i], R2=R2scontrols[i])
    regoutcontrols <- cbind(regoutcontrols, regtemp)
}

Nscontrolsnoheard <- sapply(regsetcontrolsnoheard, function(x) length(predict(x)))
R2scontrolsnoheard <- sapply(regsetcontrolsnoheard, function(x) summary(x)$r.squared)

regoutcontrolsnoheard <- rbind(names(regsetcontrolsnoheard)[1], ext(regsetcontrolsnoheard[[1]]), N=Nscontrolsnoheard[1], R2=R2scontrolsnoheard[1])
for(i in 2:length(regset)){
    regtemp <- rbind(names(regsetcontrolsnoheard)[i], ext(regsetcontrolsnoheard[[i]]), N=Nscontrolsnoheard[i], R2=R2scontrolsnoheard[i])
    regoutcontrolsnoheard <- cbind(regoutcontrolsnoheard, regtemp)
}

write.csv(regoutcontrolsnoheard, "Tables/TableC2-FergRegressionsWControls.csv")

write.csv(regout, "Tables/TableC3-FergHeardAboutRegs.csv")
write.csv(regoutcontrols, "Tables/TableC4-FergHeardAboutRegressionsWControls.csv")

percepcormatrix <- cor(sapply(perceptualqs, nalevs), use="pairwise.complete.obs")

write.csv(percepcormatrix, "Tables/TableC5-CorrelationsBetweenFergOutcomes.csv")


pdf("Figures/FigureC1-HeardAboutNoControls.pdf", width=12, height=12)
par(mfrow=c(2,2))
plot(0:1, 0:1, type="n", ylab="Should Wilson Be Charged (Mean after Recoding from 0-1)", ylim=0:1, xlab="Amount Heard About Ferguson", main="a) Wilson Should Be Charged\nBy Amount Heard About Ferguson and Race", axes=FALSE)
axis(1, at=seq(0, 1, .25), c("Nothing", "A little", "Moderate Amt.", "A lot", "Great Deal"))
axis(2, at=seq(0, 1, 1/3), c("Def. Not", "Prob. Not", "Probably", "Definitely"))
axis(1, at=c(-99,99))
axis(2, at=c(-99,99))
axis(3, at=c(-99,99))
axis(4, at=c(-99,99))
abline(h=seq(0, 1, 1/3), col="light gray", lty=3)
plotwtdinteraction(regset[[1]], "heardferg", "black", bylevnames=c("White", "Black"), placement="bottomleft", add=TRUE, linecol=c("dark gray", "black"), secol="gray")
plot(0:1, 0:1, type="n", ylab="How Likely Brown Attacked Wilson (Mean after Recoding from 0-1)", ylim=0:1, xlab="Amount Heard About Ferguson", main="b) Brown Attacked Wilson\nBy Amount Heard About Ferguson and Race", axes=FALSE)
axis(1, at=seq(0, 1, .25), c("Nothing", "A little", "Moderate Amt.", "A lot", "Great Deal"))
axis(2, at=seq(0, 1, .25), c("Not at all", "A little", "Somewhat", "Very", "Extremely"))
axis(1, at=c(-99,99))
axis(2, at=c(-99,99))
axis(3, at=c(-99,99))
axis(4, at=c(-99,99))
abline(h=seq(0, 1, .25), col="light gray", lty=3)
plotwtdinteraction(regset[[2]], "heardferg", "black", bylevnames=c("White", "Black"), placement="bottomright", add=TRUE, linecol=c("dark gray", "black"), secol="gray")
plot(0:1, 0:1, type="n", ylab="Brown Had Weapon (Mean after Recoding from 0-1)", ylim=0:1, xlab="Amount Heard About Ferguson", main="c) Brown Had a Weapon\nBy Amount Heard About Ferguson and Race", axes=FALSE)
axis(1, at=seq(0, 1, .25), c("Nothing", "A little", "Moderate Amt.", "A lot", "Great Deal"))
axis(2, at=seq(0, 1, 1/3), c("Def. Not", "Prob. Not", "Probably", "Definitely"))
axis(1, at=c(-99,99))
axis(2, at=c(-99,99))
axis(3, at=c(-99,99))
axis(4, at=c(-99,99))
abline(h=seq(0, 1, 1/3), col="light gray", lty=3)
plotwtdinteraction(regset[[3]], "heardferg", "black", bylevnames=c("White", "Black"), placement="bottomleft", add=TRUE, linecol=c("dark gray", "black"), secol="gray")
plot(0:1, 0:1, type="n", ylab="Race Role in Shooting (Mean after Recoding from 0-1)", ylim=0:1, xlab="Amount Heard About Ferguson", main="d) Race Role in Shooting\nBy Amount Heard About Ferguson and Race", axes=FALSE)
axis(1, at=seq(0, 1, .25), c("Nothing", "A little", "Moderate Amt.", "A lot", "Great Deal"))
axis(2, at=seq(0, 1, .25), c("None", "A little", "Moderate", "Large", "Enormous"))
axis(1, at=c(-99,99))
axis(2, at=c(-99,99))
axis(3, at=c(-99,99))
axis(4, at=c(-99,99))
abline(h=seq(0, 1, .25), col="light gray", lty=3)
plotwtdinteraction(regset[[4]], "heardferg", "black", bylevnames=c("White", "Black"), placement="bottomright", add=TRUE, linecol=c("dark gray", "black"), secol="gray")
dev.off()


pdf("Figures/FigureC2-FergFigureWControls.pdf", width=12, height=12)
par(mfrow=c(2,2))
plot(0:1, 0:1, type="n", ylab="Should Wilson Be Charged (Mean after Recoding from 0-1)", ylim=0:1, xlab="Amount Heard About Ferguson", main="a) Wilson Should Be Charged\nBy Amount Heard About Ferguson and Race", axes=FALSE)
axis(1, at=seq(0, 1, .25), c("Nothing", "A little", "Moderate Amt.", "A lot", "Great Deal"))
axis(2, at=seq(0, 1, 1/3), c("Def. Not", "Prob. Not", "Probably", "Definitely"))
axis(1, at=c(-99,99))
axis(2, at=c(-99,99))
axis(3, at=c(-99,99))
axis(4, at=c(-99,99))
abline(h=seq(0, 1, 1/3), col="light gray", lty=3)
plotwtdinteraction(regsetcontrols[[1]], "heardferg", "black", bylevnames=c("White", "Black"), placement="bottomleft", add=TRUE, linecol=c("dark gray", "black"), secol="gray")
plot(0:1, 0:1, type="n", ylab="How Likely Brown Attacked Wilson (Mean after Recoding from 0-1)", ylim=0:1, xlab="Amount Heard About Ferguson", main="b) Brown Attacked Wilson\nBy Amount Heard About Ferguson and Race", axes=FALSE)
axis(1, at=seq(0, 1, .25), c("Nothing", "A little", "Moderate Amt.", "A lot", "Great Deal"))
axis(2, at=seq(0, 1, .25), c("Not at all", "A little", "Somewhat", "Very", "Extremely"))
axis(1, at=c(-99,99))
axis(2, at=c(-99,99))
axis(3, at=c(-99,99))
axis(4, at=c(-99,99))
abline(h=seq(0, 1, .25), col="light gray", lty=3)
plotwtdinteraction(regsetcontrols[[2]], "heardferg", "black", bylevnames=c("White", "Black"), placement="bottomright", add=TRUE, linecol=c("dark gray", "black"), secol="gray")
plot(0:1, 0:1, type="n", ylab="Brown Had Weapon (Mean after Recoding from 0-1)", ylim=0:1, xlab="Amount Heard About Ferguson", main="c) Brown Had a Weapon\nBy Amount Heard About Ferguson and Race", axes=FALSE)
axis(1, at=seq(0, 1, .25), c("Nothing", "A little", "Moderate Amt.", "A lot", "Great Deal"))
axis(2, at=seq(0, 1, 1/3), c("Def. Not", "Prob. Not", "Probably", "Definitely"))
axis(1, at=c(-99,99))
axis(2, at=c(-99,99))
axis(3, at=c(-99,99))
axis(4, at=c(-99,99))
abline(h=seq(0, 1, 1/3), col="light gray", lty=3)
plotwtdinteraction(regsetcontrols[[3]], "heardferg", "black", bylevnames=c("White", "Black"), placement="bottomleft", add=TRUE, linecol=c("dark gray", "black"), secol="gray")
plot(0:1, 0:1, type="n", ylab="Race Role in Shooting (Mean after Recoding from 0-1)", ylim=0:1, xlab="Amount Heard About Ferguson", main="d) Perceptions of Race Role in Shooting\nBy Amount Heard About Ferguson and Race", axes=FALSE)
axis(1, at=seq(0, 1, .25), c("Nothing", "A little", "Moderate Amt.", "A lot", "Great Deal"))
axis(2, at=seq(0, 1, .25), c("None", "A little", "Moderate", "Large", "Enormous"))
axis(1, at=c(-99,99))
axis(2, at=c(-99,99))
axis(3, at=c(-99,99))
axis(4, at=c(-99,99))
abline(h=seq(0, 1, .25), col="light gray", lty=3)
plotwtdinteraction(regsetcontrols[[4]], "heardferg", "black", bylevnames=c("White", "Black"), placement="bottomright", add=TRUE, linecol=c("dark gray", "black"), secol="gray")
dev.off()


# Demographics of Completes

respdemogs <- cbind(Black=c(wpct(demo$w3.female[black]), wpct(cut(demo$w3.age[black], c(17.5, 24.5, 34.5, 44.5, 54.5, 64.5, 74.5, 999))), wpct(demo$w3.educ[black])), White=c(wpct(demo$w3.female[!black]), wpct(cut(demo$w3.age[!black], c(17.5, 24.5, 34.5, 44.5, 54.5, 64.5, 74.5, 999))), wpct(demo$w3.educ[!black])))
rownames(respdemogs) <- c("Female", "Male", "Age18-24", "Age25-34", "Age35-44", "Age45-54", "Age55-64", "Age65-74", "Age75+", levels(demo$w3.educ))

write.csv(respdemogs, "Tables/TableA1-DemographicsOfRespondents.csv")

# Distributions of Substantive Variables

subvars <- data.frame(heardferg, pq01, black)

dists <- t(sapply(subvars, function(x) c(mean(x, na.rm=TRUE), sd(x, na.rm=TRUE), sum(!is.na(x)))))
colnames(dists) <- c("Mean", "SD", "N")
write.csv(dists, "Tables/TableB1-DistributionsOfSubstantiveVariables.csv")

